package com.business.system.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.business.system.mapper.ClientMapper;
import com.business.system.mapper.ShopMapper;
import com.business.system.mapper.TbBusinessMapper;
import com.business.system.service.ITbBusinessService;
import com.business.system.vo.*;
import com.common.entity.Audtmsg;
import com.common.entity.TbBusiness;
import com.common.untils.CommonResult;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pgx
 * @since 2022-08-31
 */
@Service
public class TbBusinessServiceImpl implements ITbBusinessService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ClientMapper clientMapper;
    @Override
    public CommonResult findAllBusiness(BusinessVo businessVo) {
        Page<TbBusiness> page = new Page<>(businessVo.getCurrentPage(),businessVo.getPageSize());
        System.out.println(businessVo.getCurrentPage());
        System.out.println(businessVo.getPageSize());
        QueryWrapper<TbBusiness> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(businessVo.getBname())){
            queryWrapper.like("bname",businessVo.getBname());
        }
        if(StringUtils.hasText(businessVo.getBeginTime())){
            queryWrapper.ge("enterdate",businessVo.getBeginTime());
        }
        if(StringUtils.hasText(businessVo.getEndTime())){
            queryWrapper.le("enterdate",businessVo.getEndTime());
        }
        queryWrapper.eq("b.is_deleted",0);
        queryWrapper.eq("audtstate",1);
        queryWrapper.eq("b.auditresult",1);
        IPage<Map> businessIPage= tbBusinessMapper.selectBusinessMsg(page, queryWrapper);
        return new CommonResult(2000,"查询成功",businessIPage);
    }
    @Override
    public CommonResult detailedMsg(Integer id) {
        List<Map> list = tbBusinessMapper.detaileMsg(id);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult auditBusiness(AuditBusinessVo auditBusinessVo,String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String username = client.getString("username");
        String mobile = client.getString("mobile");
        auditBusinessVo.setBname(username);
        auditBusinessVo.setTelephone(mobile);
        //添加商家信息
        Integer row =  tbBusinessMapper.insertBusiness(auditBusinessVo);
        //添加审核记录表
        Integer row02 = tbBusinessMapper.insertAuditMsg(auditBusinessVo);
        //添加店铺
        Integer row01 =  shopMapper.addShop(auditBusinessVo);
        if(row==1&&row01==1&&row02==1) {
            return new CommonResult(2000, "添加成功", null);
        }else {
            return new CommonResult(5000, "添加失败", null);
        }
    }

    @Override
    public CommonResult findAuditMsg(AuditMsg auditMsg) {
        Page<TbBusiness> page = new Page<>(auditMsg.getCurrentPage(),auditMsg.getPageSize());
        QueryWrapper<TbBusiness> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(auditMsg.getBname())){
            queryWrapper.like("bname",auditMsg.getBname());
        }
        if(StringUtils.hasText(auditMsg.getSname())){
            queryWrapper.like("sname",auditMsg.getSname());
        }
        if(StringUtils.hasText(auditMsg.getAddress())){
            queryWrapper.like("address",auditMsg.getAddress());
        }
        if(StringUtils.hasText(auditMsg.getAuditstatr())){
            queryWrapper.eq("audtstate",auditMsg.getAuditstatr());
        }
        if(StringUtils.hasText(auditMsg.getBeginTime())){
            queryWrapper.ge("applicationdate",auditMsg.getBeginTime());
        }
        if(StringUtils.hasText(auditMsg.getEndTime())){
            queryWrapper.le("applicationdate",auditMsg.getEndTime());
        }
        queryWrapper.eq("b.is_deleted",0);
        IPage<Map> auditIPage = tbBusinessMapper.findAuditMsg(page,queryWrapper);
       if(auditIPage!=null){
           return new CommonResult(2000,"查询成功",auditIPage);
       }else {
           return new CommonResult(5000,"查询失败",null);
       }
    }

    @Override
    public CommonResult auditDetailedMsg(Integer id) {
        TbBusiness business = tbBusinessMapper.selectById(id);
        return new CommonResult(2000,"查询成功",business);
    }

    @Override
    public CommonResult passAudit(PassAuditVo passAuditVo, String token) {
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setAudtstate(1);
        tbBusiness.setAuditresult(1);
        tbBusiness.setEnterdate(LocalDateTime.now());
        Integer id =  passAuditVo.getId();
        tbBusiness.setId(id);
        //修改商家审核状态
        int row = tbBusinessMapper.updateById(tbBusiness);
        //修改店铺审核状态
        String sname = passAuditVo.getSname();
        int row01 = shopMapper.updateAudtstate(sname);
        //添加审核记录
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String name = user.getString("username");
        Integer row02 = tbBusinessMapper.setAuditMsg02(sname,name);
        //添加用户表中的商家id
        String bname = passAuditVo.getBname();
        Integer row03 = clientMapper.setClientBid(id,bname);
        if(row==1&&row01==1&&row02==1&&row03==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult turnAudit(PassAuditVo passAuditVo,String token) {
        //修改商家状态
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setAudtstate(1);
        tbBusiness.setId(passAuditVo.getId());
        tbBusiness.setEnterdate(LocalDateTime.now());
        Integer row = tbBusinessMapper.updateById(tbBusiness);
        //修改店铺状态
        String sname = passAuditVo.getSname();
        Integer row01 =shopMapper.updateAudit(sname);
        //添加审核记录
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String name = user.getString("username");
        String reasonMsg = passAuditVo.getReasonMsg();
        Integer row02 = tbBusinessMapper.setAuditMsg03(sname,name,reasonMsg);
        if(row==1&&row01==1&&row02==1){
            return new CommonResult(2000,"修改成功",null);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult findBusinessMsg(String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String bid = client.getString("bid");
        TbBusiness tbBusiness = tbBusinessMapper.findBusinessMsg(bid);
        if(tbBusiness!=null){
            return new CommonResult(2000,"查询成功",tbBusiness);
        }else {
            return new CommonResult(2000,"查询成功",null);
        }
    }

    @Override
    public CommonResult editBusinessMsg(String homeToken, AuditBusinessVo auditBusinessVo) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        Integer bid = client.getInteger("bid");
        System.out.println("++++++++++++++++++++"+bid);
        TbBusiness tbBusiness = new TbBusiness();
        tbBusiness.setId(bid);
        tbBusiness.setBicon(auditBusinessVo.getBicon());
        tbBusiness.setLicense(auditBusinessVo.getLicense());
        tbBusiness.setTelephone(auditBusinessVo.getTelephone());
        tbBusiness.setPayaccount(auditBusinessVo.getPayaccount());
        tbBusiness.setBname(auditBusinessVo.getBname());
        int row = tbBusinessMapper.updateById(tbBusiness);
        if(row==1){
            return new CommonResult(2000,"修改成功",null);
        }else {
            return new CommonResult(5000,"修改失败",null);
        }
    }

    @Override
    public CommonResult withdrawamount(double withdrawamount,String homeToken) {
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get(homeToken);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject client = (JSONObject) map.get("client");
        String bid = client.getString("bid");
        TbBusiness businessMsg = tbBusinessMapper.findBusinessMsg(bid);
        Double money = businessMsg.getMoney();
        Double newMoney = money-withdrawamount;
        Integer row = tbBusinessMapper.updateMoney(newMoney,bid);
        if(row==1){
            return new CommonResult(2000,"提现成功",null);
        }else {
            return new CommonResult(5000,"提现失败",null);
        }
    }


}
